﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EasyStep.Attributes;
using EasyStep.TestExecution.Data;

using EasyStep.TestExecution.Interfaces;
using ArtOfTest.WebAii.Core;


namespace EasyStep.TestActions.Actions.Functions
{
    [TestAction(typeof(Navigate), "To Url", RequireScreenShot = true, RequireLocator = true, RequireData = false)]
    class NavigateToUrl : ITestAction<Manager>
    {
        public void Execute(Manager browser, TestStepResult data, IExecuteDataContext context)
        {
            var url = data.Locator;
            if (string.IsNullOrEmpty(url))
            {
                data.ExecutionLog.AppendLine("Error! Can't navigate browser to empty URL!");
                data.Result = TestExecution.ExecutionResult.Failed;
                return;
            };
            if (url.StartsWith("~/"))
            {
                var uri = new Uri(browser.ActiveBrowser.Url);
                url = uri.GetLeftPart(UriPartial.Authority) + "/" + url.Remove(0, 2);
                data.ExecutionLog.AppendLine("Transform Url, been appended site urlAuthority:" + url);
            }
            var schemaEnds = url.IndexOf(Uri.SchemeDelimiter);
            if (schemaEnds > -1 && schemaEnds < 10)
                data.ExecutionLog.AppendLine("result Url:" + url);
            else
            {
                url = string.Concat(Uri.UriSchemeHttp, Uri.SchemeDelimiter, url);
                data.ExecutionLog.AppendLine("Transform Url, been appended default protocol:" + url);
            }
            if (browser.ActiveBrowser == null)
                browser.LaunchNewBrowser();
            browser.ActiveBrowser.NavigateTo(url);
        }
    }


    [TestAction(null, "Navigate",IsAbstract=true, RequireScreenShot = true, RequireLocator = false, RequireData = false)]
    class Navigate : ITestAction<Manager>
    {       
        public void Execute(Manager browser, TestStepResult data, IExecuteDataContext context)
        {
            throw new NotImplementedException();
        }
    }
}
